package com.tang.excel.oci.listener;

import com.alibaba.excel.context.AnalysisContext;
import com.alibaba.excel.event.AnalysisEventListener;
import com.tang.excel.oci.domain.Exhaust;
import com.tang.excel.oci.domain.ExhaustV1;
import com.tang.excel.oci.mapper.ExhaustMapper;

import java.util.ArrayList;
import java.util.List;

/**
 * @author Tang
 * @classname ExhaustV1Listener
 * @description [  ]
 * @date 2021/5/25 9:38
 */
public class ExhaustV1Listener extends AnalysisEventListener<ExhaustV1> {

    List<Exhaust> exhausts = new ArrayList<>(10);

    ExhaustMapper exhaustMapper;

    public ExhaustV1Listener(ExhaustMapper exhaustMapper) {
        this.exhaustMapper = exhaustMapper;
    }

    @Override
    public void invoke(ExhaustV1 exhaustV1, AnalysisContext analysisContext) {
        Exhaust e1 = new Exhaust(exhaustV1.getSaveDate(), "nox", exhaustV1.getD1(), 0);
        exhausts.add(e1);
        Exhaust e2 = new Exhaust(exhaustV1.getSaveDate(), "so2", exhaustV1.getD2(), 0);
        exhausts.add(e2);
        Exhaust e3 = new Exhaust(exhaustV1.getSaveDate(), "ss", exhaustV1.getD3(), 0);
        exhausts.add(e3);
        Exhaust e4 = new Exhaust(exhaustV1.getSaveDate(), "speed", exhaustV1.getD4(), 0);
        exhausts.add(e4);
    }

    @Override
    public void doAfterAllAnalysed(AnalysisContext analysisContext) {
        exhaustMapper.batchInsert(exhausts);
        System.out.println("解析插入完成...");
    }
}